package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.enums.DateTimePatternEnum;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.vo.UeditorResponse;
import com.cd.javacxy.common.utils.DateUtil;
import com.cd.javacxy.common.utils.ServerUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author yanghy
 * @date 2019/11/23
 */
@Controller
@RequestMapping("/file")
public class CxyFileController {

    private static final int MAX_FILE_MAX = 2 * 1024 * 1024;
    private static final int MAX_FILE_MAX1 = 3 * 1024 * 1024;

    @ResponseBody
    @PostMapping("/upload")
    public Map<String, Object> fileUpload(HttpSession session, MultipartHttpServletRequest multipartHttpServletRequest, HttpServletResponse response) {
        String realPath = ServerUtils.getRealPath() + "/upload";
        Map<String, Object> map = new HashMap<>(5);
        Iterator<String> itr = multipartHttpServletRequest.getFileNames();
        if (itr.hasNext()) {
            MultipartFile multipartFile = multipartHttpServletRequest.getFile(itr.next());
            long size = multipartFile.getSize();
            if (size > MAX_FILE_MAX) {
                map.put("responseCode", ResponseCode.BUSSINESSERROR);
                map.put("msg", "文件不能超过2M");
                return map;
            }
            String fileName = multipartFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (!"zip".equalsIgnoreCase(suffix) && !"rar".equalsIgnoreCase(suffix)) {
                map.put("responseCode", ResponseCode.BUSSINESSERROR);
                map.put("msg", "只能ZIP或RAR文件");
                return map;
            }
            String current = String.valueOf(System.currentTimeMillis());
            fileName = current + "." + suffix;
            String saveDir = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMM.getPattern());
            String savePath = saveDir + "/" + fileName;
            String fileDir = realPath + "/" + saveDir;
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = fileDir + "/" + fileName;
            File file = new File(filePath);
            try {
                multipartFile.transferTo(file);
                map.put("responseCode", ResponseCode.SUCCESS);
                map.put("savePath", savePath);
                return map;
            } catch (Exception e) {
                map.put("responseCode", ResponseCode.SERVERERROR);
                map.put("msg", "服务器异常,上传失败");
                return map;
            }
        }
        return map;
    }

    /**
     * 删除文件
     */
    @ResponseBody
    @DeleteMapping("/")
    public Map<String, Object> fileDelete(HttpSession session, String fileName, HttpServletResponse response) {
        String realPath = ServerUtils.getRealPath() + "/upload";
        Map<String, Object> map = new HashMap<>(5);
        String filePath = realPath + "/" + fileName;
        File file = new File(filePath);
        try {
            if (file.exists()) {
                file.delete();
            }
            map.put("responseCode", ResponseCode.SUCCESS.getCode());
            return map;
        } catch (Exception e) {
            map.put("responseCode", ResponseCode.SERVERERROR);
            map.put("msg", "服务器异常,删除失败");
            return map;
        }
    }


    @ResponseBody
    @PostMapping("/up/image")
    public Map<String, Object> imageUpload(HttpSession session, MultipartHttpServletRequest multirequest,
                                           HttpServletResponse response) {
        String realPath = ServerUtils.getRealPath() + "/upload";
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator<String> itr = multirequest.getFileNames();
        if (itr.hasNext()) {
            MultipartFile multipartFile = multirequest.getFile(itr.next());
            long size = multipartFile.getSize();
            if (size > MAX_FILE_MAX) {
                map.put("responseCode", ResponseCode.BUSSINESSERROR);
                map.put("msg", "文件不能超过3M");
                return map;
            }
            String fileName = multipartFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (!"JPG".equalsIgnoreCase(suffix) && !"BMP".equalsIgnoreCase(suffix) &&
                    !"gif".equalsIgnoreCase(suffix) && !"PNG".equalsIgnoreCase(suffix)) {
                map.put("responseCode", ResponseCode.BUSSINESSERROR);
                map.put("msg", "只能上传图片");
                return map;
            }
            String current = String.valueOf(System.currentTimeMillis());
            fileName = current + "." + suffix;
            String saveDir = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMM.getPattern());
            String savePath = saveDir + "/" + fileName;
            String fileDir = realPath + "/" + saveDir;
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = fileDir + "/" + fileName;
            File file = new File(filePath);
            try {
                multipartFile.transferTo(file);
                map.put("responseCode", ResponseCode.SUCCESS);
                map.put("savePath", savePath);
                return map;
            } catch (Exception e) {
                map.put("responseCode", ResponseCode.SERVERERROR);
                map.put("msg", "服务器异常,上传失败");
                return map;
            }
        }
        return map;
    }


    @ResponseBody
    @PostMapping("/up/ueditor/img")
    public UeditorResponse ueditorImageUpload(HttpSession session, MultipartHttpServletRequest multirequest,
                                              HttpServletResponse response) {
        String realPath = ServerUtils.getRealPath() + "/upload";
        UeditorResponse ueditorResponse = new UeditorResponse();
        Iterator<String> itr = multirequest.getFileNames();
        if (itr.hasNext()) {
            MultipartFile multipartFile = multirequest.getFile(itr.next());
            long size = multipartFile.getSize();
            if (size > MAX_FILE_MAX1) {
                ueditorResponse.setState("图片过大");
                return ueditorResponse;
            }
            String fileName = multipartFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (!"JPG".equalsIgnoreCase(suffix) && !"BMP".equalsIgnoreCase(suffix) &&
                    !"gif".equalsIgnoreCase(suffix) && !"PNG".equalsIgnoreCase(suffix)) {
                ueditorResponse.setState("只能是图片");
                return ueditorResponse;
            }
            String current = String.valueOf(System.currentTimeMillis());
            fileName = current + "." + suffix;
            String saveDir = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMM.getPattern());
            String savePath = saveDir + "/" + fileName;
            String fileDir = realPath + "/" + saveDir;
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = fileDir + "/" + fileName;
            File file = new File(filePath);
            try {
                multipartFile.transferTo(file);
                ueditorResponse.setState("SUCCESS");
                ueditorResponse.setUrl(this.getRealPath(multirequest) + "/upload/" + savePath);
                return ueditorResponse;
            } catch (Exception e) {
                ueditorResponse.setState("上传参数出错");
                return ueditorResponse;
            }
        }
        return ueditorResponse;
    }

    @ResponseBody
    @RequestMapping("imageUpload2Temp.action")
    public Map<String, Object> imageUpload2Temp(HttpSession session, MultipartHttpServletRequest multirequest,
                                                HttpServletResponse response) {
        String realPath = ServerUtils.getRealPath() + "/upload";
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator<String> itr = multirequest.getFileNames();
        if (itr.hasNext()) {
            MultipartFile multipartFile = multirequest.getFile(itr.next());
            long size = multipartFile.getSize();
            if (size > MAX_FILE_MAX1) {
                map.put("responseCode", ResponseCode.BUSSINESSERROR);
                map.put("msg", "文件不能超过3M");
                return map;
            }
            String fileName = multipartFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (!"JPG".equalsIgnoreCase(suffix) && !"BMP".equalsIgnoreCase(suffix) &&
                    !"gif".equalsIgnoreCase(suffix) && !"PNG".equalsIgnoreCase(suffix)) {
                map.put("responseCode", ResponseCode.BUSSINESSERROR);
                map.put("msg", "只能上传图片");
                return map;
            }
            String current = String.valueOf(System.currentTimeMillis());
            fileName = current + "." + suffix;
            String saveDir = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMM.getPattern());
            String savePath = saveDir + "/" + fileName;
            String fileDir = realPath + "/" + saveDir;
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = fileDir + "/" + fileName;
            File file = new File(filePath);
            try {
                multipartFile.transferTo(file);
                map.put("responseCode", ResponseCode.SUCCESS);
                map.put("savePath", savePath);
                return map;
            } catch (Exception e) {
                map.put("responseCode", ResponseCode.SERVERERROR);
                map.put("msg", "服务器异常,上传失败");
                return map;
            }
        }
        return map;
    }

    private String getRealPath(HttpServletRequest request) {
        String port = request.getServerPort() == 80 ? "" : ":" + request.getServerPort();
        String realpath = "http://" + request.getServerName() + port;
        return realpath;
    }
}
